#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#
# Copyright [Gtlions Lai].
# Create Date:
# Update Date:
"""summarization ahout this script.

detail ahout this script

    Class(): summarization about Class
    ...
    function(): summarization about function
    ...
"""
__authors__ = '"Gtlions Lai" <gtlions.l@qq.com>'
import sys
import os
import platform
import colorama

if platform.system() == 'Windows' or platform.system() == 'Darwin':
    from psycopg2 import *
elif platform.system() == 'Linux':
    from pygresql.pgdb import *
import time
import datetime
import dsn

db = connect(host=dsn.host, database=dsn.database, user=dsn.user, password=dsn.password)
cur = db.cursor()
cur.execute("select * from gpmg.proc_cron")
for o, p in cur.fetchall():
    if o.lower() == 'c':
        exec_sql = "select pg_cancel_backend(" + str(p) + ")"
        cur.execute(exec_sql)
        r = 'Succeed!'
        if cur.fetchone()[0]:
            print r
            cur.execute("insert into gpmg.proc_cron_log(oper_time,oper_result,oper,pid) values(%s, %s, %s, %s)",
                        (time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()), r, o, p))
            cur.execute("delete from gpmg.proc_cron where oper=%s and pid=%s", (o, p))
            db.commit()
        else:
            r = 'Failure!'
            print r + ' Wlii attempt 5 times. [PID = ' + str(p) + ' ]'
            for i in range(5):
                print str(i + 1) + ' Times. '
                cur.execute(exec_sql)
                if cur.fetchone()[0]:
                    r = 'Succeed!'
                    print r
                    break
                time.sleep(5)
                if i == 4:
                    print r + ' Yet attempt 5 times. [PID = ' + str(p) + ' ]'
            cur.execute("insert into gpmg.proc_cron_log(oper_time,oper_result,oper,pid) values(%s, %s, %s, %s)",
                        (time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()), r, o, p))
            cur.execute("delete from gpmg.proc_cron where oper=%s and pid=%s", (o, p))
            db.commit()
    elif o.lower() == 'k':
        exec_sql = "select pg_terminate_backend(" + str(p) + ")"
        cur.execute(exec_sql)
        r = 'Succeed!'
        if cur.fetchone()[0]:
            print r
            cur.execute("insert into gpmg.proc_cron_log(oper_time,oper_result,oper,pid) values(%s, %s, %s, %s)",
                        (time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()), r, o, p))
            cur.execute("delete from gpmg.proc_cron where oper=%s and pid=%s", (o, p))
            db.commit()
        else:
            r = 'Failure!'
            print r + ' Wlii attempt 5 times. [PID = ' + str(p) + ' ]'
            for i in range(5):
                print str(i + 1) + ' Times. '
                cur.execute(exec_sql)
                if cur.fetchone()[0]:
                    r = 'Succeed!'
                    print r
                    break
                time.sleep(5)
                if i == 4:
                    print r + ' Yet attempt 5 times. [PID = ' + str(p) + ' ]'
            cur.execute("insert into gpmg.proc_cron_log(oper_time,oper_result,oper,pid) values(%s, %s, %s, %s)",
                        (time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()), r, o, p))
            cur.execute("delete from gpmg.proc_cron where oper=%s and pid=%s", (o, p))
            db.commit()
